package com.yuanfeng.job.job;

import com.yuanfeng.commoms.util.DateUtils;
import com.yuanfeng.job.mapper.MerchantGoodsCommonJobMapper;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsTimeShelfJob
 * @Description 商品定时上架
 * @Author liulh
 * @Date 2023/5/17 11:25
 * @Version 1.0
 **/
public class GoodsTimeShelfJob implements BaseJob {

    @Autowired
    private MerchantGoodsCommonJobMapper merchantGoodsCommonJobMapper;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            //查询出所有定时发布的商品
            List<Map<String, Object>> list = merchantGoodsCommonJobMapper.queryGoodsTimeShelf();
            if (null != list && !list.isEmpty()) {
                for (Map<String, Object> map : list) {
                    if (null != map.get("commonSellTime")) {
                        LocalDateTime commonSellTime = (LocalDateTime) map.get("commonSellTime");
                        Boolean orverTimeStart = DateUtils.compareDate2(commonSellTime);
                        //判断当前商品发布的时间是否大于当前时间
                        if (!orverTimeStart) {
                            Integer commonId = ((Long) map.get("commonId")).intValue();
                            merchantGoodsCommonJobMapper.updateGoodsPutaway(commonId);
                            merchantGoodsCommonJobMapper.updateGoodsBase(commonId);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
